package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.spa.infrastructure.repository.condition.UserInfoCondition;
import com.spa.infrastructure.repository.mapper.UserInfoMapper;
import com.spa.infrastructure.repository.po.UserInfoPO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
@Slf4j
public class UserInfoRepositoryService extends MPJBaseServiceImpl<UserInfoMapper, UserInfoPO> {


    public List<UserInfoPO> list(UserInfoCondition condition) {
        return list(wrapper(condition));
    }

    public Long countByCondition(UserInfoCondition condition) {
        return count(wrapper(condition));
    }

    private LambdaQueryWrapper<UserInfoPO> wrapper(UserInfoCondition condition) {
        LambdaQueryWrapper<UserInfoPO> wrapper = Wrappers.lambdaQuery();
        wrapper.le(StringUtils.hasText(condition.getEndTime()), UserInfoPO::getRegTime, condition.getEndTime());
        wrapper.ge(StringUtils.hasText(condition.getBeginTime()), UserInfoPO::getRegTime, condition.getBeginTime());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getId()),UserInfoPO::getId,condition.getId());
        wrapper.in(CollectionUtil.isNotEmpty(condition.getIds()),UserInfoPO::getId,condition.getIds());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getUserId()),UserInfoPO::getUserId,condition.getUserId());
        wrapper.eq(StringUtils.hasText(condition.getPhone()),UserInfoPO::getPhone,condition.getPhone());
        if(StringUtils.hasText(condition.getName())){
            wrapper.like(UserInfoPO::getName,condition.getName());
        }
        return wrapper;
    }
    public UserInfoPO userInfo(UserInfoCondition condition){
        LambdaQueryWrapper<UserInfoPO> wrapper = wrapper(condition);
        List<UserInfoPO> poList = list(wrapper);
        if(CollUtil.isNotEmpty(poList)){
            return poList.get(0);
        }
        return null;
    }

    public Page<UserInfoPO> page (UserInfoCondition userInfoCondition){
        return page(Page.of(userInfoCondition.getCurrent(),userInfoCondition.getSize()),wrapper(userInfoCondition));
    }

    /**
     * 重置新用户
     * @param UserId
     * @return
     */
    public boolean logOffUser(Long UserId,String phone){
        // 生成一个没有分隔符的日期格式
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MMddHHmm");
        String formattedDate = localDateTime.format(dateTimeFormatter);
        // 更新用户表
        LambdaUpdateWrapper<UserInfoPO> userInfoPOLambdaQueryWrapper = Wrappers.lambdaUpdate();
        userInfoPOLambdaQueryWrapper.set(UserInfoPO::getPhone,phone+"D"+formattedDate);
        userInfoPOLambdaQueryWrapper.eq(UserInfoPO::getId,UserId);
        return update(userInfoPOLambdaQueryWrapper);
    }
}
